package io.renren.modules.jsf.service.impl;

import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.jsf.dao.TContentDao;
import io.renren.modules.jsf.entity.TContentEntity;
import io.renren.modules.jsf.service.TContentService;

import static io.renren.common.utils.ShiroUtils.getUserEntity;
import static io.renren.common.utils.ShiroUtils.getUserId;


@Service("tContentService")
public class TContentServiceImpl extends ServiceImpl<TContentDao, TContentEntity> implements TContentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<TContentEntity> queryWrapper = new QueryWrapper<>();
        if (params.get("key") != null) {
            queryWrapper.like("content", params.get("key"))
                    .or().like("user_name", params.get("key"));
        }
        IPage<TContentEntity> page = this.page(
                new Query<TContentEntity>().getPage(params),
                queryWrapper
        );

        SysUserEntity userEntity = getUserEntity();
        if (getUserId() != 1) {
            List<TContentEntity> collect = page.getRecords().stream().filter(t -> {
                return t.getUserName().equals(userEntity.getUsername());
            }).collect(Collectors.toList());

            page.setRecords(collect);
            return new PageUtils(page);
        }
        return new PageUtils(page);
    }

}